﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Controls.HtmlControls;
using QXBook.Service;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Product
{
    public partial class ProductList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(27);

            lmc.AddEditLinkUrl = "ProductEdit.aspx";
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkShipments = false;
            lmc.EnableLinkResave = false;
            if (!Page.IsPostBack)
            {
                this.TextBind(null);
            }

            lmc.EditHandler += new EventHandler(lmc_EditHandler);
            lmc.DeleteHandler += new EventHandler(lmc_DeleteHandler);
            lbtnAll.Click += new EventHandler(lbtnAll_Click);
            lbtnIsPublish.Click += new EventHandler(lbtnIsPublish_Click);
            lbtnNoPublish.Click += new EventHandler(lbtnNoPublish_Click);
        }

        void lmc_EditHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int productID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    productID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                Response.Redirect("ProductEdit.aspx?ProductID=" + productID);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一条");
            }
            else
            {
                base.ResponseErch("请选择一条资讯");
            }
        }

        void lmc_DeleteHandler(object sender, EventArgs e)
        {
            int flags = 0;
            string articles = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    articles += hidID.Value + ",";
                    flags++;
                }
            }

            if (flags > 0)
            {
                ServiceFactory.CreateInstance<ArticleService>().DeleteList(articles);
                ProductCondition condition = GetPageCondition();
                this.TextBind(condition);

                base.ResponseErch("删除成功");
            }
            else
            {
                base.ResponseErch("请选择一个或者多个");
            }
        }

        void lbtnAll_Click(object sender, EventArgs e)
        {
            ProductCondition condition = new ProductCondition();
            this.TextBind(condition);
        }

        void lbtnNoPublish_Click(object sender, EventArgs e)
        {
            hidIsPublish.Value = CommonUtils.ToInt(YesNo.No).ToString();
            ProductCondition condition = GetPageCondition();
            this.TextBind(condition);
        }

        void lbtnIsPublish_Click(object sender, EventArgs e)
        {
            hidIsPublish.Value = CommonUtils.ToInt(YesNo.No).ToString();
            ProductCondition condition = GetPageCondition();
            this.TextBind(condition);
        }

        private ProductCondition GetPageCondition()
        {
            ProductCondition condition = new ProductCondition();
            if (!string.IsNullOrEmpty(hidIsPublish.Value))
            {
                condition.IsPublish = (YesNo)CommonUtils.ToInt(hidIsPublish.Value);
            }

            return condition;
        }

        private void TextBind(ProductCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<ProductInfo> list = ServiceFactory.CreateInstance<ProductService>().GetModelList(ref ps, condition);

            this.rptList.DataSource = list;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ProductInfo product = (ProductInfo)e.Item.DataItem;
            Span sId = (Span)e.Item.FindControl("sId");
            HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
            Span sTitle = (Span)e.Item.FindControl("sTitle");
            Span sClassName = (Span)e.Item.FindControl("sClassName");
            Span sChecked = (Span)e.Item.FindControl("sChecked");
            Span sUpdateDate = (Span)e.Item.FindControl("sUpdateDate");

            sId.Text = product.ProductID.ToString();
            hidID.Value = product.ProductID.ToString();
            sTitle.Text = product.Title;
            sClassName.Text = product.Categorie.CategoryName;
            sChecked.Text = product.IsPublish == YesNo.Yes ? "<img src='../images/icon/deled.gif' />" : string.Empty;
            sUpdateDate.Text = product.UpdateDate.ToString();
        }
    }
}